package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;

public abstract class TGBottomSheetDialog {
    public final static String FROM_SOLAR_VIDEO_MODE = "from_solar_video_mode";
    public final static String FROM_LAMP_WIFI_VIDEO = "from_lamp_wifi_video";
    @NonNull
    protected BottomSheetDialog bottomSheetDialog;
    @NonNull
    protected Context context;
    protected String from;
    public Object object;
    public TGBottomSheetDialog(@NonNull Context context) {
        initDialog(context, null, null);
    }

    public TGBottomSheetDialog(@NonNull Context context, String from) {
        initDialog(context, from, null);
    }

    public TGBottomSheetDialog(@NonNull Context context, String from, Object object) {
        initDialog(context, from, object);
    }

    private void initDialog(@NonNull Context context, String from, Object object){
        this.context = context;
        this.from = from;
        this.object = object;
        bottomSheetDialog = new BottomSheetDialog(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams") View view = inflater.inflate(getContentView(), null, false);
        bottomSheetDialog.setContentView(view);
        bottomSheetDialog.setCanceledOnTouchOutside(false);
        bottomSheetDialog.setCancelable(false);
        initView(view);
    }
    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        bottomSheetDialog.setOnDismissListener(listener);
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        bottomSheetDialog.setCanceledOnTouchOutside(cancel);
    }


    public void setCancelable(boolean cancelable) {
        bottomSheetDialog.setCancelable(cancelable);
    }

    protected abstract int getContentView();
    protected abstract void initView(@NonNull View view);
    public void show() {
        if (bottomSheetDialog.isShowing()) {
            bottomSheetDialog.dismiss();
        }
        bottomSheetDialog.show();
    }

    public void dismiss() {
        if (bottomSheetDialog.isShowing()) {
            bottomSheetDialog.dismiss();
        }
    }
}
